

/**
 * 创建时间: 2020/7/15 18:24
 * 作者:lixu
 * 功能描述:cmdId相关的工具方法
 * 修改时间:
 * 修改描述:
 */
public class CommandUtils {
    /**
     * 服务器msg响应成功code
     */
    private static var SERVER_RESP_SUCCESS_CODE_MAP:[Int] = [Int].init()
    /**
     * 服务器msg响应失败code
     */
    private static var SERVER_RESP_FAIL_CODE_MAP:[Int]  = [Int].init()
//    static {
//        initMap();
//    }

     static func initMap() {
        
        //synchronized
        let Queue = DispatchQueue.init(label: "im.CommandUtils")
        Queue.sync {
        
            let s: TimeInterval = Date.init(timeIntervalSinceNow: 0).timeIntervalSince1970
            let sMilli = s*1000
            if (SERVER_RESP_SUCCESS_CODE_MAP.isEmpty) {
                
                
                let mirrorRespSuccCmdIdID =  Mirror(reflecting: RespSuccCmdID())

                for field in mirrorRespSuccCmdIdID.children  {
                    
                    SERVER_RESP_SUCCESS_CODE_MAP.append(field.value as! Int)
                    
                }
            }

            if (SERVER_RESP_FAIL_CODE_MAP.isEmpty) {
               
                let mirrorRespFailCmdID =  Mirror(reflecting: RespFailCmdID())
                for field in mirrorRespFailCmdID.children  {
                    
                    SERVER_RESP_FAIL_CODE_MAP.append(field.value as! Int)
                    
                }
            }
            
                  
            let e: TimeInterval = Date.init(timeIntervalSinceNow: 0).timeIntervalSince1970
//            var eMilli = CLongLong(round(e*1000))
            let eMilli = e*1000
            IMLog.i(tag:"CommandUtils.initMap", msg: "耗时:\(eMilli - sMilli)ms successMapSize:\( SERVER_RESP_SUCCESS_CODE_MAP.count)  failMapSize:\( SERVER_RESP_FAIL_CODE_MAP.count)");
        }
        
      
    }

    /**
     * 根据cmdId判断当前消息是否是服务器响应
     *
     * @param cmdId
     * @return
     */
    public static func isServerResp( cmdId:Int) ->Bool{
        return SERVER_RESP_SUCCESS_CODE_MAP.contains(cmdId) || SERVER_RESP_FAIL_CODE_MAP.contains(cmdId);
    }

    /**
     * 判断是否是client发送消息成功后的响应
     *
     * @param cmdId
     * @return
     */
    public static func isSuccessResp( cmdId:Int)->Bool {
        return SERVER_RESP_SUCCESS_CODE_MAP.contains(cmdId);
    }

    /**
     * 判断是否需要断开IM
     * 根据cmdID判断,如果是登录,退出房间或切换房间失败,需要断开IM
     *
     * @param cmdId
     * @return
     */
    public static func isNeedDisConnent( cmdId:Int)->Bool {
        return cmdId == RespFailCmdID.init().CMDID_LOGOUT_ROOM_FAIL_RESP || cmdId == RespFailCmdID.init().CMDID_CHANGE_ROOM_FAIL_RESP || cmdId == RespFailCmdID.init().CMDID_LOGIN_FAIL_RESP;
    }
}
